package com.yanqu.road.server.cmd.unionwar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.logic.pb.CrossRankPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.unionwar.CrossUnionWarProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarActivityMgr;
import com.yanqu.road.server.manager.unionwar.CrossUnionWarMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_CROSS_UNION_WAR_JOIN_UNION_SYNC, desc = "商会战参赛商会信息同步")
public class CrossUnionWarJoinUnionSyncCmd extends CrossUnionCommand {

    private CrossUnionWarProto.CrossUnionWarJoinUnionListSyncMsg reqMsg;
    private List<String> unionUidList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        reqMsg = CrossUnionWarProto.CrossUnionWarJoinUnionListSyncMsg.parseFrom(packet.getBytes());
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        int activityId = reqMsg.getActivityId();
        ActivityInfo activityInfo = CrossUnionWarActivityMgr.getOpenActivity(activityId);
        for(CrossUnionWarProto.CrossUnionWarJoinUnionTempMsg tempMsg : reqMsg.getDataListList()){
            UnionBaseInfo unionBaseInfo = CrossRankPb.parseUnionBaseInfo(tempMsg.getUnionBaseTemp());
            CrossUnionWarMgr.updateUnionInfo(activityId, tempMsg.getUnionUid(), serverId, unionBaseInfo, groupId);
            unionUidList.add(tempMsg.getUnionUid());
        }
    }

    @Override
    public void sendData() {
        CrossUnionWarProto.CrossUnionWarJoinUnionListSyncRespMsg.Builder respMsg = CrossUnionWarProto.CrossUnionWarJoinUnionListSyncRespMsg.newBuilder();
        respMsg.setActivityId(reqMsg.getActivityId());
        respMsg.addAllUnionUidList(unionUidList);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_UNION_WAR_JOIN_UNION_SYNC_RESP, respMsg);
        MessageHelper.sendPacket(serverId, 0, pbMsg);
    }
}
